var Toast4JS;
(function(){

    var content = null;
    var isVisible = false;
    var startTime = 0;
    var showTime = 0;
    var width_height = [0,0];//存放content数据的总宽高，【0】宽  【1】高
    var fontSize;

    var SW;
    var SH;
    var SplitStringWidth;
    var drawCanvas;

    Toast4JS = function(){
        isVisible = false;
    }

    Toast4JS.prototype.firstInit = function(initSW , initSH, initFontSize, initDrawCanvas){
        SW = initSW;
        SH = initSH;
        SplitStringWidth = SW*2/3;
        fontSize = initFontSize;
        drawCanvas = initDrawCanvas;
    }

    Toast4JS.prototype.buildToast = function(initContent, intiShowTime){
        showTime = intiShowTime;
        content = splitString(initContent, SplitStringWidth, "\n", fontSize, drawCanvas, width_height);

        startTime = Date.now();
        isVisible = true;
    }

    Toast4JS.prototype.logic = function(){
        if(!isVisible)
            return false;
        if(Date.now() - startTime > showTime){
            this.clear();
            return true;
        }
        return false;
    }

    Toast4JS.prototype.paint = function(){
        if(!isVisible)
            return;
        var space_h = 15;
        var space_v = 10;

        if(null != content){
            drawRoundRect(drawCanvas, 2, "#ffffff", (SW - width_height[0]-space_h*2)>>1, 600, width_height[0]+space_h*2 ,space_v*2 + width_height[1], 10, true, "#000000");
            drawStringArray(drawCanvas, content, SW>>1, 600+space_v, "center", fontSize, "#ffffff");
        }
    }

    Toast4JS.prototype.clear = function(){
        isVisible = false;
        if(null != content){
            for (var i = 0; i < content.length; i++) {
                content[i] = null;
            }
            content = null;
        }
    }

})();

var AlertDialog;
(function(){

    var iconImg;
    var content_title;
    var content_message;
    var weight_height_title = [0,0];
    var weight_height_message = [0,0];

    var isVisible = false;
    var fontSize_title;
    var fontSize_message;

    var SW;
    var SH;
    var SplitStringWidth;
    var drawCanvas;
    var bgImg;
    var drawStartX;
    var drawStartY;

    var butOkClickListener;
    var butCancelClickListener;

    AlertDialog = function(){
        isVisible = false;
    }

    AlertDialog.prototype.firstInit = function(initSW , initSH, initBgImg, initFontSizeTitle, initFontSizeMessage, initDrawCanvas){
        SW = initSW;
        SH = initSH;
        bgImg = initBgImg;
        SplitStringWidth = 240;
        fontSize_title = initFontSizeTitle;
        fontSize_message = initFontSizeMessage;
        drawCanvas = initDrawCanvas;

        drawStartX = SW-480>>1;
        drawStartY = SH-280>>1;
    }

    AlertDialog.prototype.setIconImg = function(initIconImg){
        iconImg = initIconImg;
    }

    AlertDialog.prototype.setTitle = function(initTitle){
        content_title = splitString(initTitle, SplitStringWidth, "\n", fontSize_title, drawCanvas, weight_height_title);
    }

    AlertDialog.prototype.setMessage = function(initMessage){
        content_message = splitString(initMessage, SplitStringWidth, "\n", fontSize_message, drawCanvas, weight_height_message);
    }

    AlertDialog.prototype.setOkButtonListener = function(initButOkClickListener){
        butOkClickListener = initButOkClickListener;
    }

    AlertDialog.prototype.setCancelButtonListener = function(initButCancelClickListener){
        butCancelClickListener = initButCancelClickListener;
    }

    AlertDialog.prototype.show = function(){
        isVisible = true;
    }

    AlertDialog.prototype.paint = function(){
        if(!isVisible)
            return;

        if(null != content_title){
            drawCanvas.drawImage(bgImg, drawStartX, drawStartY);
            drawCanvas.drawImage(iconImg, drawStartX+70, drawStartY+70);
            drawStringArray(drawCanvas, content_title, drawStartX+187, drawStartY+80, "left", fontSize_title, "#ffff00");
            drawStringArray(drawCanvas, content_message, drawStartX+187, drawStartY+80+weight_height_title[1]+10, "left", fontSize_message, "#ffc101");
        }
    }

    AlertDialog.prototype.input = function(tX, tY) {
        if(!isVisible)
            return false;
        else{
            if(isTouch_inArea(tX,tY, drawStartX+56, drawStartY+181, drawStartX+178, drawStartY+228)){
                //确定
                butOkClickListener();
//                this.clear();
            }
            else if(isTouch_inArea(tX,tY, drawStartX+267, drawStartY+181, drawStartX+390, drawStartY+228)){
                //取消
                butCancelClickListener();
                this.clear();
            }
            return true;
        }
    }

    AlertDialog.prototype.clear = function(){
        isVisible = false;
        if(null != content_title){
            for (var i = 0; i < content_title.length; i++) {
                content_title[i] = null;
            }
            content_title = null;
        }
        if(null != content_message){
            for (var i = 0; i < content_message.length; i++) {
                content_message[i] = null;
            }
            content_message = null;
        }
    }

})();

var Loading4JS;
(function(){

    var content = null;
    var isVisible = false;

    var fontSize;
    var SW;
    var SH;
    var drawCanvas;
    var imgBg;
    var imgLoading;
    var changeAngle = 0;

    Loading4JS = function(){
        isVisible = false;
    }

    Loading4JS.prototype.firstInit = function(initSW , initSH, initFontSize, initDrawCanvas, initBg, initImg){
        SW = initSW;
        SH = initSH;
        fontSize = initFontSize;
        drawCanvas = initDrawCanvas;
        imgBg = initBg;
        imgLoading = initImg;
    }

    Loading4JS.prototype.showLoading = function(initContent){
        content = initContent;
        isVisible = true;
    }

    Loading4JS.prototype.closeLoading = function(){
        this.clear();
    }

    Loading4JS.prototype.logic = function(){
        if(!isVisible)
            return false;

        changeAngle = changeAngle + Math.PI/12;
        if(changeAngle >= 2*Math.PI){
            changeAngle = 0;
        }

        return true;
    }

    Loading4JS.prototype.paint = function(){
        if(!isVisible)
            return;
        drawCanvas.drawImage(imgBg, 0, 0);

        drawCanvas.save();
        drawCanvas.translate(234+22, 344+22);
        drawCanvas.rotate(changeAngle);
        drawCanvas.drawImage(imgLoading, -22, -22);
        drawCanvas.restore();

        if(null != content){
            drawCanvas.font = fontSize+"px Arial";
            drawCanvas.textAlign = "center";
            drawCanvas.textBaseline = "top";
            drawCanvas.fillStyle = "#ffffff";
            drawCanvas.fillText(content, 256, 344+44+10);
            drawCanvas.font = "14px Arial";
        }
    }

    Loading4JS.prototype.clear = function(){
        isVisible = false;
        content = null;
    }

})();


var ChargeDialog;
(function(){

    var imgBg;
    var imgTitle;
    var imgBack;

    var imgBarBg;
    var imgX;
    var imgFontBig;
    var imgFontSmall;
    var imgIconDiamond;
    var imgIconCoin;
    var imgBtnDiamond;
    var imgBtnCoin;
    var imgUpperPage;
    var imgNextPage;

    var pageCapacity = 6;//一页的容量
    var pageTotal = 0;//共几页
    var pageIndex = 0;//当前第几页
    var optionTotal = 0;//共几个充值选项

    var isVisible = false;
    var isChargeDiamond;

    var chargeCoinArray;

    var SW;
    var SH;
    var drawCanvas;
    var drawStartX;
    var drawStartY;
    var areaDis = 16;

    ChargeDialog = function(){
        isVisible = false;
    }

    ChargeDialog.prototype.firstInit = function(initSW , initSH, imgBg_i, imgTitle_i, imgBack_i, imgBarBg_i, imgX_i, imgFontBig_i, imgFontSmall_i, imgIconDiamond_i, imgIconCoin_i, imgBtnDiamond_i, imgBtnCoin_i, imgUpperPage_i, imgNextPage_i, initDrawCanvas){
        SW = initSW;
        SH = initSH;

        imgBg = imgBg_i;
        imgTitle = imgTitle_i;
        imgBack = imgBack_i;
        imgBarBg = imgBarBg_i;
        imgX = imgX_i;
        imgFontBig = imgFontBig_i;
        imgFontSmall = imgFontSmall_i;
        imgIconDiamond = imgIconDiamond_i;
        imgIconCoin = imgIconCoin_i;
        imgBtnDiamond = imgBtnDiamond_i;
        imgBtnCoin = imgBtnCoin_i;
        imgUpperPage = imgUpperPage_i;
        imgNextPage = imgNextPage_i;

        drawCanvas = initDrawCanvas;

        chargeCoinArray = [[5000,5],[10000,10],[22000,20],[55000,50],[90000,80],[120000,100],[150000,140],[200000,180]];

        drawStartX = 53;
        drawStartY = 158;
    }

    ChargeDialog.prototype.show = function(isShowDiamong){
        isChargeDiamond = isShowDiamong;
        if(isShowDiamong){
            //显示充值钻石界面
            optionTotal = chargeDiamongArray.length;
            if(optionTotal == 0){
                //从服务器加载数据
                loading4JS.show();
            }
            else{
                pageIndex = 0;
                pageTotal = optionTotal%pageCapacity==0?zhengchu(optionTotal,pageCapacity):zhengchu(optionTotal,pageCapacity)+1;
            }
        }
        else{
            //显示充值金币界面
            pageIndex = 0;
            optionTotal = chargeCoinArray.length;
            pageTotal = optionTotal%pageCapacity==0?zhengchu(optionTotal,pageCapacity):zhengchu(optionTotal,pageCapacity)+1;
        }
        isVisible = true;
    }

    ChargeDialog.prototype.paint = function(){
        if(!isVisible)
            return;

        drawCanvas.drawImage(imgBg, 0, 0);
        drawCanvas.drawImage(imgBack,21,16);
        drawCanvas.drawImage(imgTitle,108,84);

        if(pageIndex > 0){
            drawCanvas.drawImage(imgUpperPage, 53, 650);
        }
        if(pageIndex < pageTotal-1){
            drawCanvas.drawImage(imgNextPage, 294, 650);
        }

        if(optionTotal > 0){
            var startIndex = pageIndex*pageCapacity;
            for(var i=startIndex; i< startIndex+6; i++){
                if(i >= optionTotal){
                    break;
                }
                var barStartY = drawStartY+(60+areaDis)*(i-startIndex);
                drawCanvas.drawImage(imgBarBg, drawStartX, barStartY);
                if(isChargeDiamond){
                    drawCanvas.drawImage(imgIconDiamond, drawStartX+26, barStartY+11);
                    drawCanvas.drawImage(imgBtnDiamond, drawStartX+302, barStartY+4);
                    drawNum(drawCanvas, chargeDiamongArray[i][0], drawStartX+132, barStartY+20, 21, 24, imgFontBig);
                    drawNum(drawCanvas, chargeDiamongArray[i][1], drawStartX+346, barStartY+32, 10, 13, imgFontSmall);
                }
                else{
                    drawCanvas.drawImage(imgIconCoin, drawStartX+35, barStartY+1);
                    drawCanvas.drawImage(imgBtnCoin, drawStartX+302, barStartY+4);
                    drawNum(drawCanvas, chargeCoinArray[i][0], drawStartX+132, barStartY+20, 21, 24, imgFontBig);
                    drawNum(drawCanvas, chargeCoinArray[i][1], drawStartX+346, barStartY+32, 10, 13, imgFontSmall);
                }
                drawCanvas.drawImage(imgX, drawStartX+106, barStartY+22);
            }
        }

    }

    ChargeDialog.prototype.input = function(tX, tY) {
        if(!isVisible)
            return false;
        else{
            if(isTouch_inArea(tX,tY, 21, 16, 21+73, 16+44)){
                //back
                this.clear();
            }
            else if(isTouch_inArea(tX,tY, 53, 650, 53+157, 650+32)){
                //上一页
                if(pageIndex > 0){
                    pageIndex--;
                }
            }
            else if(isTouch_inArea(tX,tY, 294, 650, 294+157, 650+32)){
                //下一页
                if(pageIndex < pageTotal-1){
                    pageIndex++;
                }
            }
            else if(isTouch_inArea(tX,tY, 355, 162, 355+96, 162+(60+areaDis)*6)){
                var rowIndex = zhengchu(tY-162,60+areaDis);
                if(tY-162<=rowIndex*(60+areaDis)+52){
                    //选中
                    var index = pageIndex*pageCapacity + rowIndex;
                    if(index <optionTotal){
                        if(isChargeDiamond){
                            BuyType = index;
                            openChargePage();
                        }
                        else{
                            if(myDiamond >= chargeCoinArray[index][1]){
                                myCoin = myCoin + chargeCoinArray[index][0];
                                myDiamond = myDiamond - chargeCoinArray[index][1];

                                loading4JS.showLoading("正在提交请求，请等待！");
                                updateBaseInfo(function(){
                                    loading4JS.closeLoading();
                                    toast4JS.buildToast("成功购买金币！",1500);
                                });
                            }
                            else{
                                toast4JS.buildToast("钻石不足！",1500);
                            }
                        }
                    }
                }
            }
            return true;
        }
    }

    ChargeDialog.prototype.isShow = function(){
        return isVisible;
    }

    ChargeDialog.prototype.clear = function(){
        isVisible = false;
    }

})();



var LottoScreen;
(function(){

    var alphaImg;
    var backImg;
    var bgImg;
    var bgNpcImg;
    var bgLightImg;
    var bgRoundImg;
    var btnCostImg;
    var btnFreeImg;
    var lightImgs;

    var isStartLotto;
    var lightIndex;

    var isVisible = false;
    var drawCanvas;
    var drawStartX;
    var drawStartY;

    var hasRewardArray = [0,1,2,3,5,6,7,8,9];
    var currentStep;
    var totalStep;

    var effect_count;

    LottoScreen = function(){
        isVisible = false;
    }

    LottoScreen.prototype.firstInit = function(initalphaImg, initbackImg, initbgImg , initbgNpcImg, initbgLightImg, initbgRoundImg, initbtnCostImg, initbtnFreeImg, initlightImgs, initDrawCanvas){
        alphaImg = initalphaImg;
        backImg = initbackImg;
        bgImg = initbgImg;
        bgNpcImg = initbgNpcImg;
        bgLightImg = initbgLightImg;
        bgRoundImg = initbgRoundImg;
        btnCostImg = initbtnCostImg;
        btnFreeImg = initbtnFreeImg;
        lightImgs = initlightImgs;
        drawCanvas = initDrawCanvas;

        drawStartX = SW-320>>1;
        drawStartY = 304;
    }

    LottoScreen.prototype.show = function(){
        isVisible = true;
        isStartLotto = false;
        lightIndex = 0;
    }

    LottoScreen.prototype.logic = function(){
        if(!isVisible)
            return false;
        if(isStartLotto){
            effect_count++;
            if(effect_count%3==2){
                currentStep++;
                lightIndex++;
                if(lightIndex>11){
                    lightIndex = 0;
                }
                if(currentStep == totalStep){
                    isStartLotto = false;
                    var strGotText;
                    switch(lightIndex){
                        case 0:
                            myToolArray[3] = myToolArray[3]+1;
                            strGotText = "恭喜获得1个强化进阶";
                            break;
                        case 1:
                            myDiamond = myDiamond + 2;
                            strGotText = "恭喜获得2颗钻石";
                            break;
                        case 2:
                            myCoin = myCoin + 2000;
                            strGotText = "恭喜获得2000金币";
                            break;
                        case 3:
                            myToolArray[1] = myToolArray[1]+1;
                            strGotText = "恭喜获得1个超能炸弹";
                            break;
                        case 5:
                            myDiamond = myDiamond + 5;
                            strGotText = "恭喜获得5颗钻石";
                            break;
                        case 6:
                            myToolArray[2] = myToolArray[2]+1;
                            strGotText = "恭喜获得1个无敌冲击波";
                            break;
                        case 7:
                            myCoin = myCoin + 5000;
                            strGotText = "恭喜获得5000金币";
                            break;
                        case 8:
                            myDiamond = myDiamond + 10;
                            strGotText = "恭喜获得10颗钻石";
                            break;
                        case 9:
                            myToolArray[1] = myToolArray[1]+1;
                            strGotText = "恭喜获得1个超能炸弹";
                            break;
                    }
                    hasLottoChance = false;

                    loading4JS.showLoading("正在提交请求，请等待！");
                    UpdateTool(function(){
                        loading4JS.closeLoading();
                        toast4JS.buildToast(strGotText, 1500);
                    });
                }
            }
        }
        return true;
    }

    LottoScreen.prototype.paint = function(){
        if(!isVisible)
            return;

        drawCanvas.drawImage(alphaImg, 0, 0);
        drawCanvas.drawImage(backImg,21,16);
        drawCanvas.drawImage(bgNpcImg, 171, 119);
        drawCanvas.drawImage(bgImg, drawStartX, drawStartY);

        drawCanvas.drawImage(lightImgs[lightIndex], drawStartX+81, drawStartY+78);
        drawCanvas.drawImage(bgRoundImg, drawStartX+125, drawStartY+122);
        if(isStartLotto){
            if(effect_count%3==0){
                drawCanvas.drawImage(bgLightImg, drawStartX+61, drawStartY+55);
            }
        }
        else{
            if(hasLottoChance){
                drawCanvas.drawImage(btnFreeImg, drawStartX+87, drawStartY+275);
            }
            else{
                drawCanvas.drawImage(btnCostImg, drawStartX+87, drawStartY+275);
            }
        }
    }

    LottoScreen.prototype.input = function(tX, tY) {
        if(!isVisible)
            return false;
        else{
            if(!isStartLotto){
                if(isTouch_inArea(tX,tY, drawStartX+87, drawStartY+275, drawStartX+87+151, drawStartY+275+63)){
                    if(hasLottoChance){
                        //免费抽奖
//                    lightIndex = 0;
                        currentStep = 0;
                        var stopIndex = hasRewardArray[randomNextInt(0,8)];
                        var roundTime = randomNextInt(1,4);
                        totalStep = stopIndex-lightIndex+roundTime*12;
                        effect_count = 0;

                        isStartLotto = true;
                    }
                    else{
                        //钻石抽奖
                        if(myDiamond >= 5){
                            myDiamond = myDiamond-5;
                            this.initStart();
                        }
                        else{
                            toast4JS.buildToast("钻石不足", 1500);
                        }
                    }
                }
                else if(isTouch_inArea(tX,tY, 21, 16, 21+73, 16+44)){
                    //back
                    this.clear();
                }
            }
            return true;
        }
    }

    LottoScreen.prototype.initStart = function(){
        //                    lightIndex = 0;
        currentStep = 0;
        var stopIndex = hasRewardArray[randomNextInt(0,8)];
        var roundTime = randomNextInt(1,4);
        totalStep = stopIndex-lightIndex+roundTime*12;
        effect_count = 0;

        isStartLotto = true;
    }

    LottoScreen.prototype.clear = function(){
        isVisible = false;
    }

})();